<?php

namespace INT\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use INT\AdminBundle\Entity\Matiere;
use INT\AdminBundle\Form\MatiereType;
class MatiereController extends Controller
{
    public function indexAction()
    {
        $matieres=  $this->getDoctrine()->getEntityManager()->getRepository('INTAdminBundle:Matiere')->findAll();
        return $this->render('INTAdminBundle:Matiere:index.html.twig',array('matieres'=>$matieres));
    }
    public function addEditMatiereAction(){
        $request=$this->getRequest();
        $em=$this->getDoctrine()->getEntityManager();
        $repository=$em->getRepository('INTAdminBundle:Matiere');
        $id= $request->get('id');
        if($id == '-1'){
            $matiere = new Matiere();
        }else{
            $matiere =  $repository->find($id);
        }
        $form = $this->createForm(new MatiereType(), $matiere);
        if($request->isMethod('post')){
            $form->bind($request);
            if($form->isValid()){
            $matiere=$form->getData();
            $em->persist($matiere);
            $em->flush();
            $matieres=  $this->getDoctrine()->getEntityManager()->getRepository('INTAdminBundle:Matiere')->findAll();
        return $this->render('INTAdminBundle:Matiere:list.html.twig',array('matieres'=>$matieres));
            }
        }
        return $this->render('INTAdminBundle:Matiere:newEdit.html.twig',array('form'=>$form->createView(),'id'=>$id));
    }
    
        public function supprimerMatiereAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getEntityManager();
        $repository = $em->getRepository('INTAdminBundle:Matiere');
        $id = $request->get('id');
        $matiere = $repository->find($id);
        $em->remove($matiere);
        $em->flush();
        $matieres = $this->getDoctrine()->getEntityManager()->getRepository('INTAdminBundle:Matiere')->findAll();
        return $this->render('INTAdminBundle:Matiere:list.html.twig', array('matieres' => $matieres));
    }
}
